package cn.com.jinhao.provider.business.service.impl;

import cn.com.jinhao.hotline.commons.exception.BusinessErrorException;
import cn.com.jinhao.hotline.commons.vo.AttachRequestVO;
import cn.com.jinhao.provider.business.entity.Attach;
import cn.com.jinhao.provider.business.entity.Tape;
import cn.com.jinhao.provider.business.mapper.AttachMapper;
import cn.com.jinhao.provider.business.service.AttachService;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * hotline-spring-cloud
 * <p>Description: </p>
 *
 * @author ShaneMew
 * @version 1.0
 * @date 2020/6/2
 * @since 2020/6/2
 */
@Service
public class AttachServiceImpl implements AttachService {
    @Resource
    AttachMapper attachMapper;

    @Override
    public void add(AttachRequestVO attachRequestVO) throws Exception {
        Attach attach = new Attach();
        BeanUtil.copyProperties(attachRequestVO, attach);
        int result = attachMapper.insertSelective(attach);
        if (result == 0) {
            throw new BusinessErrorException("附件新增失败!");
        }
    }

    @Override
    public void delete(String code) throws Exception {
        Example example = new Example(Attach.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("code", code);
        int result = attachMapper.deleteByExample(example);
        if (result == 0) {
            throw new BusinessErrorException("附件删除失败!");
        }
    }

    @Override
    public List<Attach> selectByHostCode(String hostCode) {
        Example example = new Example(Attach.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("hostCode", hostCode);
        return attachMapper.selectByExample(example);
    }

    @Override
    public void addBatch(List<AttachRequestVO> attachRequestVOS) throws Exception {
        List<Attach> attaches = new ArrayList<>();
        attachRequestVOS.forEach(item -> {
            Attach attach = new Attach();
            BeanUtil.copyProperties(item, attach);
            attaches.add(attach);
        });
        int result = attachMapper.insertList(attaches);
        if (result == 0) {
            throw new BusinessErrorException("增加附件失败!");
        }
    }
}
